package com.xiaoshuidi.cloud.module.report.service.bigdata;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoshuidi.cloud.module.report.constant.DeviceTypeEnum;
import com.xiaoshuidi.cloud.module.report.constant.TotalTransactionDetailsTypeEnum;
import com.xiaoshuidi.cloud.module.report.controller.bigdata.vo.*;
import com.xiaoshuidi.cloud.module.report.dal.DO.ApartmentDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.RoomHouseDO;
import com.xiaoshuidi.cloud.module.report.dal.mapper.bigdata.BiRoomHouseMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.bigdata.OverviewTransactionMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.bigdata.RentalDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class RentalDataServiceImpl implements RentalDataService {

    private final RentalDataMapper rentalDataMapper;
    private final BiRoomHouseMapper biRoomHouseMapper;
    private final OverviewTransactionMapper overviewTransactionMapper;


    @Override
    public List<ApartmentVO> selectApartmentAll() {
        List<ApartmentVO> list = CollectionUtil.newArrayList(ApartmentVO.builder().name("全部").build());
        List<ApartmentVO> collect = rentalDataMapper.selectList().stream().map(m -> new ApartmentVO(m.getId(),
                m.getName(), m.getIsConcentrated())).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(collect)){
            list.addAll(collect);
        }
        return list;
    }

    @Override
    public ApartmentHouseStateStatisticsVO getHouseStateStatisticsByAptId(Long apartmentId) {
        ApartmentHouseStateStatisticsVO vo = rentalDataMapper.getHouseStateStatisticsByAptId(apartmentId);
        LambdaQueryWrapper<ApartmentDO> lambdaQueryWrapper =
                new LambdaQueryWrapper<ApartmentDO>().eq(apartmentId != null, ApartmentDO::getId, apartmentId)
                .eq(ApartmentDO::getIsConcentrated, false);
        //分散式
        vo.setDisperseCount(rentalDataMapper.selectCount(lambdaQueryWrapper));
        lambdaQueryWrapper.clear();
        //集中式
        vo.setConcentrateCount(rentalDataMapper.selectCount(lambdaQueryWrapper.eq(apartmentId != null,
                        ApartmentDO::getId, apartmentId)
                .eq(ApartmentDO::getIsConcentrated, true)));
        return vo;
    }


    @Override
    public HouseDeviceAnalysisVO getHouseDeviceAnalysis(Long apartmentId) {
        //门店下所有房源 排除公区
        Long houseCount = biRoomHouseMapper.selectCount(
                new LambdaQueryWrapper<RoomHouseDO>().and(f -> f.ne(RoomHouseDO::getRoomType, 4).or().isNull(RoomHouseDO::getRoomType))
                        .eq(apartmentId != null, RoomHouseDO::getApartmentId, apartmentId));
        List<Map<String, Object>> maps = rentalDataMapper.groupHouseDeviceByAptId(apartmentId);
        HouseDeviceAnalysisVO vo = HouseDeviceAnalysisVO.builder().houseCount(houseCount).build();

        maps.stream().forEach(f -> {
            Long deviceCount = Long.parseLong(f.get("device_count").toString());
            switch (DeviceTypeEnum.getEnumByNode(f.get("device_type"))) {
                case AMMETER:
                    vo.setAmmeterCount(deviceCount);
                    break;
                case COLD_WATER_METER:
                    vo.setColdWaterMeterCount(deviceCount);
                    break;
                case HOT_WATER_METER:
                    vo.setHotWaterMeterCount(deviceCount);
                    break;
                case DOOR_LOCK:
                    vo.setDoorLockCount(deviceCount);
                    break;
                default:
                    break;
            }

        });
        return vo;
    }

    @Override
    public ContractRentDistributionVO contractRentDistributionByAptId(Long apartmentId) {
        return rentalDataMapper.contractRentDistributionByAptId(apartmentId);
    }

    @Override
    public ContractRentDistributionVO paymentProportionByAptId(Long apartmentId) {
        return rentalDataMapper.paymentProportionByAptId(apartmentId);
    }

    @Override
    public CustomerAgeIntervalStepVO customerAgeIntervalStepByAptId(Long apartmentId) {
         CustomerAgeIntervalStepVO customerAgeIntervalStepVO =
                rentalDataMapper.customerAgeIntervalStepByAptId(apartmentId);

        //获取交易金额  交易金额与交易明细保持一致
        TotalTransactionDetailsVO totalTransactionDetails =
                overviewTransactionMapper.getTotalTransactionDetails(
                                null, null, apartmentId,
                        TotalTransactionDetailsTypeEnum.REVENUE.getNode());
        customerAgeIntervalStepVO.setTotalAmount(totalTransactionDetails.getTotalAmount());
        //计算总数  统计过程中不符和统计的数据需要过滤 参考区间值获取总和为主
        customerAgeIntervalStepVO.setTotalCount(
                customerAgeIntervalStepVO.getFirstStep() + customerAgeIntervalStepVO.getSecondStep() +
                        customerAgeIntervalStepVO.getThirdStep() + customerAgeIntervalStepVO.getSixthStep() +
                        customerAgeIntervalStepVO.getFourthStep() + customerAgeIntervalStepVO.getFifthStep()
                        );

        return customerAgeIntervalStepVO;
    }
}
